const app = getApp()
import {formatRichText} from '../../utils/util'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import api from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product:{},
  },
  //兑换
  pay:api.throttle(function (e) {
    let _this = this
    wx.requestSubscribeMessage({
      tmplIds: ['pb7x9TGH6KiwHssu4vSwlPAy0QgC2illmQ8XLET_cps'],
      success (res) { 
        console.log(res)
        _this.pay1(e)
      },
      fail(res){
        console.log(res)
        _this.pay1(e)
      }
    })
  }),

  async pay1(e){
    let data={}
    data['flag'] = 'gift_get'
    data['giftid'] = e.currentTarget.dataset.giftid
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      Toast({
        type: 'success',
        message: '兑换成功',
        onClose: () => {
          wx.navigateBack({
            delta: 1
          })  
        },
      });

      // this.getDetail(e.currentTarget.dataset.giftid) 
      
    }else {
      Toast(message)
    }
  },
  //详情
  async getDetail (giftid) {
    let data={}
    data['flag'] = 'gift_malls_delial'
    data['giftid'] = giftid
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result[0].giftimg = [this.loadImg(result[0].giftimg[0])]
      result[0].des = formatRichText(result[0].des)
      this.setData({
        product:result[0],
      })
    
    }else {
      Toast(message)
    }
  },
  //图片加载
  loadImg(img){
    let url =  api.checkurl(img)
    return url
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDetail(options.giftid)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})